package exercise;

import java.util.*;

/*
4.选做题
a.  给定一个长度,随机产生一个该长度的字符串,由大写,小写字母以及数字组成
b.把一个字符串的大写字母放到字符串的后面  其他字符位置不变
如:AkleBiCeilD   ==> kleieilABCD
 */
public class ex2 {
    public static void main(String[] args) {
        String string = methodB(5);
        System.out.println(string);
//        for (int i = 3; i < 10; i++)
//            System.out.println("长度为" + i + ": " + methodA(i));
//        System.out.println("------------------------------------");
//        System.out.println(methodB("AkleBiCeilD"));
    }

    public static String methodA(int len) {
        if (len < 3) {
            System.out.println("长度至少为3");
            len = 3;
        }
        Random r = new Random();
        ArrayList<Character> list = new ArrayList<>();
        //保证有大小写字母、数字
        list.add((char) (r.nextInt(26) + 'A'));
        list.add((char) (r.nextInt(26) + 'a'));
        list.add((char) (r.nextInt(10) + '0'));
        //生成剩余部分
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        chars = chars + chars.toLowerCase() + "0123456789";
        for (int i = 3; i < len; i++) {
            list.add(chars.charAt(r.nextInt(chars.length())));
        }
        //打乱顺序
        Collections.shuffle(list);
        StringBuilder sb = new StringBuilder();
        for (Character c : list) {
            sb.append(c);
        }
        return sb.toString();
    }

    public static String methodB(int len) {
        Random r = new Random();
        r.nextInt();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < len; i++) {
            int n = r.nextInt(62) + 1;
            if (n <= 10) {
                n += 47;//数字
            } else if (n <= 36) {
                n += 54;//大写字母
            } else {
                n += 60;//小写字母
            }
            sb.append((char) n);
        }
        return sb.toString();
    }

    public static String methodB(String str) {
        StringBuilder prefix = new StringBuilder();
        StringBuilder suffix = new StringBuilder();
        for (char c : str.toCharArray()) {
            if (c >= 'A' && c <= 'Z') {// 大写字母
                suffix.append(c);
            } else {//小写字母
                prefix.append(c);
            }
        }
        return prefix.append(suffix).toString();
    }
}
